package dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.MembersinfoDao;
import entity.Membersinfo;

public class MemberinfoDaoImpl extends BaseDao implements MembersinfoDao{
	
	private Connection conn=null;
	private PreparedStatement pstmt=null;
	private ResultSet rs=null;

	@Override
	public List<Membersinfo> list() {
		List<Membersinfo> list=new ArrayList<Membersinfo>();
		try {
			conn=super.getConnection();
			String sql="select * from membersinfo";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Membersinfo mem=new Membersinfo();
				mem.setId(rs.getInt("id"));
				mem.setMname(rs.getString("mname"));
				mem.setMgender(rs.getString("mgender"));
				mem.setMage(rs.getInt("mage"));
				mem.setMaddress(rs.getString("maddress"));
				mem.setMemail(rs.getString("memail"));
				list.add(mem);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		
		return list;
	}

	@Override
	public Membersinfo findById(int id) {
		Membersinfo mem=null;
		try {
			conn=super.getConnection();
			String sql="select * from membersinfo where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, id);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				mem=new Membersinfo();
				mem.setId(rs.getInt("id"));
				mem.setMname(rs.getString("mname"));
				mem.setMgender(rs.getString("mgender"));
				mem.setMage(rs.getInt("mage"));
				mem.setMaddress(rs.getString("maddress"));
				mem.setMemail(rs.getString("memail"));
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return mem;
	}

	@Override
	public int update(Membersinfo mem) {
		int result=0;
		try {
			conn=super.getConnection();
			String sql="update membersinfo set mname=?,mgender=?,mage=?,maddress=?,memail=? where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, mem.getMname());
			pstmt.setObject(2, mem.getMgender());
			pstmt.setObject(3, mem.getMage());
			pstmt.setObject(4, mem.getMaddress());
			pstmt.setObject(5, mem.getMemail());
			pstmt.setObject(6, mem.getId());
			result=pstmt.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

	@Override
	public int detele(int id) {
		int result=0;
		try {
			conn=super.getConnection();
			String sql="delete from membersinfo where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, id);
			result=pstmt.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;

	}
}
